<?php

namespace app\zftpay\controller;

use app\admin\controller\Service;
use app\BaseController;
use app\Constants;
use app\job\ZftSettleConfirmOrder;
use app\service\alipayWapService;
use app\service\curlService;
use app\service\propertyService;
use app\service\serviceService;
use app\service\settleService;
use app\service\webService;
use app\service\zftService;
use app\zftpay\service\apiService;
use dh2y\qrcode\QRcode;
use think\facade\Cache;
use think\facade\Db;
use think\facade\Log;
use think\facade\Queue;
use think\Image;
ini_set('max_execution_time','0');
ini_set('memory_limit', '4096M'); // 例如，将内存限制设置为256MB
class Test extends BaseController
{
    public function csv2(){
        $data=Db::table("order_compare")
            ->limit(21000,1000)->select();
        foreach ($data as $info) {
            echo $info["id"]." ";
            $order=Db::table("zft_order_success")
                ->where("out_trade_no",$info["out_trade_no"])
                ->field("id,pay_money,out_trade_no")
                ->find();
            if(empty($order)){

                Db::table("order_compare")
                    ->where("id",$info["id"])
                    ->update(["remark"=>"订单号不存在"]);
            }else{
                if($order["pay_money"]!=$info["compare_money"]){
                    Db::table("order_compare")
                        ->where("id",$info["id"])->update(["pay_money"=>$order["pay_money"]]);
                }
            }
        }
    }
    public function csv(){

        $filename = '/www/wwwroot/zaozao/public/5655.csv'; // CSV文件的路径



        $handle = fopen($filename, "r");
        if ($handle === false) {
            die("无法打开文件！");
        }

// 读取整个文件到数组中
        $data = [];
        while (($row = fgetcsv($handle, 1000, ",")) !== FALSE) {
            $add["out_trade_no"]=$row[0];
            $add["compare_money"]=$row[1];
            $order=Db::table("order_compare")
                ->where("out_trade_no",$add["out_trade_no"])
                ->find();
            if(empty($order)){
                Db::table("order_compare")->insert($add);
            }
        }

        exit;
        fclose($handle);

        // 获取前100条数据
        $first100Rows = array_slice($data, 0, 500);

        // 输出结果
        foreach ($first100Rows as $row) {
            $sn=$row[0];
            $money=$row[1];
            $order=Db::table("zft_order_success")
                ->where("out_trade_no",$sn)
                ->where("pay_money",$money)->find();
            if(empty($order)){
                echo $row[0] . " " . $row[1] . "</br>"; // 假设每行有三个字段
            }
        }
        exit;
        // 打开文件
        if (($handle = fopen($filename, "r")) !== FALSE) {
            while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
                $sn=$data[0];
                $money=$data[1];
                $order=Db::table("zft_order_success")
                    ->where("out_trade_no",$sn)
                    ->where("pay_money",$money)->find();
                if(empty($order)){
                    echo $data[0] . " " . $data[1] . "</br>"; // 假设每行有三个字段
                }
                // 处理每一行数据
                // $data是一个数组，包含了CSV文件中的一行数据
                // 例如，输出第一行数据
                echo $data[0] . " " . $data[1] . "</br>"; // 假设每行有三个字段

            }
            fclose($handle); // 关闭文件
        }

    }
    public function doNotifyJob($order_uid,$notify_con,$community_id){
        $url=(new serviceService())->getNotifyUrlByComId($community_id);
        $res=(new curlService())->curl_post($url,$notify_con,[]);
        Log::write($res,"doNotifyJob");
        print("<info>ZftNotifyUrl Job doJob. job Data is: ".var_export($res,true)."</info> \n");
        Db::table("zft_order_success")
            ->where("uid",$order_uid)
            ->update(["notify_time"=>date('Y-m-d H:i:s')]);
        if($res=="success"){
            return true;
        }else{
            $queueName  	  = "app\job\ZftNotifyUrl";
            $jobData       	  = [ 'order_uid' =>$order_uid,"alipay_notify_con"=>$notify_con,"community_id"=>$community_id] ;
            $resu= \think\facade\Queue::push($queueName,$jobData);
            return false;
        }

    }
    public function yangzl2(){
        $key=env("APP.ZFT_KEY");
        echo $key;
        //echo "success";
    }

    public function notifyTest(){
        $data=Db::table("zft_order_success")
            ->where("notify_time",null)
            //->where("uid","<","120277")
            ->select();
        foreach($data as $info){
            echo $info["uid"];echo "<br/>";
            $queueName  	  = "app\job\ZftNotifyUrl";
            $jobData       	  = [ 'order_uid' =>$info["uid"],"alipay_notify_con"=>$info["alipay_notify_con"],"community_id"=>$info["community_id"]] ;
            $resu= \think\facade\Queue::push($queueName,$jobData);
        }
        //var_dump($data);
    }
    public function yangzl(){
        $data=Db::table("zft_order_success")
            //->where("uid","<","120277")
            ->where("settle_status",0)->select();
        //var_dump($data);
        foreach($data as $info){
            echo $info["uid"];echo "<br/>";
                 $jobQueueName  	  = "app\job\ZftSettleConfirmOrder";
                 $jobData       	  = [ 'id' =>$info["uid"] ] ;
                 $res= \think\facade\Queue::push($jobQueueName,$jobData);
        }
//        var_dump($notifyurl_alipay);
        // for ($i = 804; $i <808; $i++) {
        //     $jobQueueName  	  = "app\job\ZftSettleConfirmOrder";
        //     $jobData       	  = [ 'id' =>$i ] ;
        //     $res= \think\facade\Queue::push($jobQueueName,$jobData);
        //     var_dump($res);
        //     echo "\r\n";
        // }
        
        // $jobQueueName  	  = "app\job\ZftSettleConfirmOrder";
        // $jobData       	  = [ 'id' =>797 ] ;
        // $res= \think\facade\Queue::push($jobQueueName,$jobData);
        // var_dump($res);
        //$info=Db::table("zft_order_success")->where("uid","2272")->find();

        echo "yangzl";
    }

    public function bindQuery(){
//        $data= array(
//            'type'=>"loginName",
//            'account'=>"xc85456324@163.com",
//            'name'=>"杭州西宸物业服务有限公司",
//            'memo'=>"",
//
//            "out_request_no"=>date("YmdHis").rand(1000,9999),
//        );
        $data= array(
//            'type'=>"loginName",
//            'account'=>"aliwykd@163.com",
//            'name'=>"杭州西宸物业服务有限公司",
            'memo'=>"",

            "out_request_no"=>date("YmdHis").rand(1000,9999),
        );
        $api=new alipayWapService();
        $res=$api->royaltyRelationBatchQuery(json_encode($data));
        echo "<pre>";
        var_dump($res);
    }
    public function bind(){
        $api=new zftService();
        $data= array(
            'type'=>"loginName",
            'account'=>"xc85456324@163.com",
            'name'=>"杭州西宸物业服务有限公司",
            'memo'=>"",
            'token'=>"202009BB8c8b7a1f312343708117675277305D93",
            "out_request_no"=>date("YmdHis").rand(1000,9999),
        );
        $res=$api->royaltyRelationBind(json_encode($data));
        echo "<pre> ";
        var_dump($res);

    }



}